package com.bee.bootstrap.system.service.impl;

import com.bee.bootstrap.resource.entity.Resource;
import com.bee.bootstrap.resource.service.IResourceService;
import com.bee.bootstrap.system.pojo.Cascader;
import com.bee.bootstrap.system.service.IInterfaceService;
import com.bee.common.core.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liekkas
 * @since 2018-04-09
 */
@Service
public class InterfaceServiceImpl implements IInterfaceService {


    @Autowired
    private IResourceService resourceServiceImpl;

    private void iteration(Resource resource,Cascader parent){
        List<Cascader> childrens=new ArrayList<>();
        resource.getChildrensView().forEach(children->{
            Cascader cascader=new Cascader();
            cascader.setLabel(children.getName());
            cascader.setValue(String.valueOf(children.getId()));
            childrens.add(cascader);
        });
        parent.setChildren(childrens);
        for(int i=0;i<resource.getChildrensView().size();i++){
            if(resource.getChildrensView().get(i).getChildrensView().size()>0)
                this.iteration(resource.getChildrensView().get(i),childrens.get(i));
            else{
                childrens.get(i).setChildren(new ArrayList<>());
            }
        }
    }

    @Override
    public Result findResourcesCascader() {
        Result result=resourceServiceImpl.selectTreeView();
        List<Resource> list= (List<Resource>) result.getObj();
        List<Cascader> root=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(Resource resource:list){
                Cascader cascader=new Cascader();
                cascader.setLabel(resource.getName());
                cascader.setValue(resource.getId().toString());
                root.add(cascader);
                this.iteration(resource,cascader);
            }
        }
        return Result.success(root);

    }
}
